package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.TipoAquisicao;

public class TipoAquisicaoDaoImpl implements TipoAquisicaoDao {
	Connection c;

	public TipoAquisicaoDaoImpl() {
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}

	@Override
	public void salvaTipoAquisicao(TipoAquisicao tipoAquisicao)
			throws SQLException {
		String sql = "INSERT INTO tiposDeAquisicoes VALUES (?,?,?,?)";
		PreparedStatement ps = c.prepareStatement(sql);

		ps.setInt(1, tipoAquisicao.getIdAquis());
		ps.setString(2, tipoAquisicao.getTA());
		ps.setString(3, tipoAquisicao.getTTA());
		ps.setString(4, tipoAquisicao.getPTA());

		ps.execute();
		ps.close();

	}

	@Override
	public void editaTipoAquisicao(TipoAquisicao tipoAquisicao)
			throws SQLException {
		String sql = "UPDATE tiposDeAquisicoes SET TA = ?, " + "TTA = ?, "
				+ "PTA = ? WHERE idAquis = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, tipoAquisicao.getTA());
		ps.setString(2, tipoAquisicao.getTTA());
		ps.setString(3, tipoAquisicao.getPTA());
		ps.setInt(4, tipoAquisicao.getIdAquis());
		ps.execute();
		ps.close();

	}

	@Override
	public void excluiTipoAquisicao(TipoAquisicao tipoAquisicao)
			throws SQLException {
		String sql = "DELETE tiposDeAquisicoes WHERE idAquis = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tipoAquisicao.getIdAquis());
		ps.execute();
		ps.close();

	}

	@Override
	public TipoAquisicao pesquisaTipoAquisicao(TipoAquisicao tipoAquisicao)
			throws SQLException {
		TipoAquisicao ta = new TipoAquisicao();
		String sql = "SELECT idAquis, TA, TTA, PTA FROM tiposDeAquisicoes WHERE idAquis = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tipoAquisicao.getIdAquis());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {

			ta.setIdAquis(rs.getInt("idAquis"));
			ta.setTA(rs.getString("TA"));
			ta.setTTA(rs.getString("TTA"));
			ta.setPTA(rs.getString("PTA"));

		}
		ps.close();
		rs.close();
		return ta;
	}

}
